<?php

namespace App\Http\Controllers\Bap;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Slide;
use App\Models\Currency;
use App\Models\CurrencyLog;

class IndexController extends Controller
{
    /**
     * 轮播图
     * @return \Illuminate\Http\JsonResponse
     */
    public function banner_list(Request $request)
    {
        $data = Slide::where('del', 1)->orderBy('order')->select('title', 'img', 'webpage')->get();
        foreach($data as $val) {
            $val->img = config('alioss.host'). '/' . $val->img;
        }
        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }

    /**
     * 首页币种行情
     */
    public function market_list(Request $request)
    {
        //获取交易币种（排除USDT）
        $orders = Currency::with('order')->whereIn('is_standard', [0, 2])->orderBy('orderby', 'DESC')->get();
        $data = [];
        $map = ['type' => 1];
        foreach ($orders as $order) {
            $arr = $order->select('id', 'logo', 'name', 'name_en')->find($order->id)->toArray();
            $arr['logo'] = config('alioss.host') . $arr['logo'];

            $another = $order->order()->where($map)->get();

            //最高价
            //$max_price = $order->order()->where($map)->max('price') ?? 0;
            $max_price = $another->max('price') ?? 0;
            //最低价
            //$min_price = $order->order()->where($map)->min('price') ?? 0;
            $min_price = $another->min('price') ?? 0;
            //成交总价
            //$sum_price = $order->order()->where($map)->sum('total_price') ?? 0;
            $sum_price = $another->sum('total_price') ?? 0;
            //成交总量
            //$order_count = $order->order()->where($map)->sum('num');
            $order_count = $another->sum('num') ?? 0;
            //最新价
            $lasted_order = $order->order()->where($map)->orderBy('updated_at', 'DESC')->first();
            $lasted_price = $lasted_order['price'] ?? 0;
            //收盘价
            $close_price = (new CurrencyLog())->get_last_close_price($order->id);
            //涨幅 = (最新价格-上一个交易日的收盘价)/上个交易日的收盘价*100%
            if (!$close_price || !$lasted_price) {
                $gains = '0%';
            } else {
                $gains = number_format(($lasted_price - $close_price) / $close_price*100, 2).'%';
            }
            
            if($order_count < 10000){
                $order_count = intval(round($order_count));
            }else{
                $order_count = price_format($order_count/10000).'万';
            }
            //按照汇率换算成人民币
            $exchange_rate = UsdRate() ?? 6.3; //汇率
            $lasted_price_cny = $lasted_price * $exchange_rate; 
            $arr['lasted_price_cny'] = price_format($lasted_price_cny);
            $arr['max_price'] = '$'.price_format($max_price);
            $arr['min_price'] = '$'.price_format($min_price);
            $arr['sum_price'] = price_format($sum_price);
            $arr['order_count'] = $order_count;
            $arr['lasted_price'] = price_format($lasted_price);
            $arr['gains'] = $gains;

            array_push($data, $arr);
        }

        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }
}
